import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import AdminLayout from '../views/admin/AdminLayout.vue'
import InventoryView from '../views/admin/InventoryView.vue'
import CaretakerView from '../views/CaretakerView.vue'
import CustomerView from '../views/CustomerView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: LoginView
    },
    {
      path: '/admin',
      component: AdminLayout,
      children: [
        {
          path: '',
          redirect: '/admin/inventory'
        },
        {
          path: 'inventory',
          name: 'admin-inventory',
          component: InventoryView
        },
        {
          path: 'orders',
          name: 'admin-orders',
          component: () => import('../views/admin/OrdersView.vue')
        },
        {
          path: 'users',
          name: 'admin-users',
          component: () => import('../views/admin/UsersView.vue')
        },
        {
          path: 'reports',
          name: 'admin-reports',
          component: () => import('../views/admin/ReportsView.vue')
        }
      ]
    },
    {
      path: '/caretaker',
      name: 'caretaker',
      component: CaretakerView
    },
    {
      path: '/customer',
      name: 'customer',
      component: CustomerView
    }
  ]
})

// 全局前置守卫
// router.beforeEach((to, from, next) => {
//   const isLoggedIn = localStorage.getItem('token')
//   console.log('路由守卫 - 目标路径:', to.path)
//   console.log('路由守卫 - 是否已登录:', !!isLoggedIn)
  
//   // 如果访问的不是登录页，且没有登录，则重定向到登录页
//   if (to.path !== '/' && !isLoggedIn) {
//     console.log('路由守卫 - 未登录，重定向到登录页')
//     next('/')
//   } else {
//     console.log('路由守卫 - 允许访问:', to.path)
//     next()
//   }
// })

export default router
